package com.deadletter.deadletter.customer;

import com.deadletter.deadletter.config.TestConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 死信队列消费者
 */
@Component
public class DeadLetterCustomer {

    /**
     * 死信消费者A
     * @param channel
     * @param message
     */
    @RabbitListener(queues = TestConfig.DEAD_LETTER_QUEUEA_NAME)
    public void deadLetterCustomerA(Channel channel, Message message){

        //这个消息不会进行如死心队列
        String msg = new String(message.getBody());

        System.out.println("死信消费者A开始消费消息：" + msg);

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 业务消费者B
     * @param channel
     * @param message
     */
    @RabbitListener(queues = TestConfig.DEAD_LETTER_QUEUEB_NAME)
    public void deadLetterCustomerB(Channel channel, Message message){

        String msg = new String(message.getBody());

        System.out.println("死信消费者B开始消费消息：" + msg);

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}
